<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: Boolean,
  default: () => {
    return false
  },
  playedText: {
    type: String,
    default: () => {
      return '数据匹配中'
    }
  }
})

const displayedText = ref('...')
let intervalId = null
const startTyping = (text = '...') => {
  displayedText.value = ''
  let index = 0
  intervalId = setInterval(() => {
    if (index < text.length) {
      displayedText.value += text[index]
      index++
    } else {
      index = 0
      displayedText.value = ''
    }
  }, 350)
}

watch(
  () => props.playedText,
  () => {
    clearInterval(intervalId)
    startTyping()
  }
)
</script>

<template>
  <transition name="fade">
    <div class="mask" v-if="visible">
      <div class="loading-box">
        <div class="steps">{{ props.playedText + (props.playedText === '薪资计算完成' ? '' : displayedText) }} <span></span> </div>
        <div class="loading">
          <span style="--i: 1"></span>
          <span style="--i: 1"></span>
          <span style="--i: 2"></span>
          <span style="--i: 3"></span>
          <span style="--i: 4"></span>
          <span style="--i: 4"></span>
          <span style="--i: 5"></span>
          <span style="--i: 5"></span>
          <span style="--i: 6"></span>
          <span style="--i: 6"></span>
          <span style="--i: 7"></span>
          <span style="--i: 7"></span>
          <span style="--i: 8"></span>
          <span style="--i: 8"></span>
          <span style="--i: 8"></span>
        </div>
      </div>
    </div>
  </transition>
</template>

<style scoped lang="less">
.mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.8);
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.loading-box {
  display: flex;
  justify-content: center;
  align-items: center;
  //渐变色
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.9);
  flex-direction: column;
  position: fixed;
  top: 10%;
  width: 600px;
  height: 300px;
  padding: 10px 20px;
  border-radius: 12px;
  font-size: 16px;
  color: #333;
  z-index: 99;
  .steps {
    height: 32px;
    margin-bottom: 12px;
    font-size: 16px;
  }
}

.loading {
  position: relative;
  display: flex;
  gap: 15px;
}

.loading span {
  position: relative;
  width: 5px;
  height: 20px;
}

.loading span::before {
  content: '';
  position: absolute;
  inset: 0;
  animation: animate 8s linear infinite;
  animation-delay: calc(var(--i) * 0.1s);
}

@keyframes animate {
  0% {
    background-color: #738cfa;
    box-shadow: 0 0 5px #738cfa, 0 0 10px #738cfa, 0 0 14px #738cfa, 0 0 20px #738cfa;
    rotate: 0deg;
  }
  20% {
    background-color: #738cfa;
    box-shadow: 0 0 5px #738cfa, 0 0 10px #738cfa, 0 0 14px #738cfa, 0 0 20px #738cfa;
    rotate: 0deg;
  }
  40% {
    background-color: #738cfa;
    box-shadow: 0 0 5px #738cfa, 0 0 10px #738cfa, 0 0 14px #738cfa, 0 0 20px #738cfa;
    rotate: 90deg;
    transform-origin: bottom;
    filter: hue-rotate(0deg);
  }
  80% {
    background-color: #738cfa;
    box-shadow: 0 0 5px #738cfa, 0 0 10px #738cfa, 0 0 14px #738cfa, 0 0 20px #738cfa;
    rotate: 90deg;
    transform-origin: bottom;
    filter: hue-rotate(360deg);
  }
  90% {
    background-color: #51e456;
    box-shadow: none;
    rotate: 0;
  }
  90.1%,
  100% {
    background-color: #51e456;
    box-shadow: none;
  }
}
</style>
